package org.erepublik.company.scanner;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBException;

import org.erepublik.company.ECountry;
import org.erepublik.company.Region;
import org.erepublik.company.generated.RawMaterialT;
import org.erepublik.company.generated.RegionTTT;
import org.erepublik.company.generated.RegionsTT;
import org.erepublik.company.searchCriteria.ERawRegion;
import org.erepublik.company.utils.WebpageReader;
import org.erepublik.company.xml.XMLParsersers;
import org.json.JSONArray;
import org.json.JSONException;

public class RegionsScanner {

    public RegionsScanner() {

    }
    
    public ArrayList<Region> scannRegions() {
        ArrayList<Region> regionList = new ArrayList<Region>();
        for (ECountry country : ECountry.values()) {
            regionList.addAll(scannRegionsFromCountry(country));
        }
        return regionList;
    }
    
    
    private ArrayList<Region> scannRegionsFromCountry(ECountry country) {
        System.out.println("Scanning regions from " + country);
        InputStream stream = null;
        RegionsTT products = null;
        try {
            stream = WebpageReader.getStreamToIrl("http://api.erepublik.com/v2/feeds/countries/"+ country.getErepublikId() +"/regions");
            products = XMLParsersers.parseRegions(stream);
        } catch (IOException e) {
            e.printStackTrace();
            return new ArrayList<Region>();
        } catch (JAXBException e) {
            e.printStackTrace();
            return new ArrayList<Region>();
        }
        
        return getRegionsFromJaxb(products);
    }

    
    private ArrayList<Region> getRegionsFromJaxb(RegionsTT regions) {     
        ArrayList<Region> regionList = new ArrayList<Region>();
        for (RegionTTT region : regions.getRegion()) {
            Region newRegion = new Region();
            newRegion.setId( region.getId() );
            newRegion.setName( region.getName() );
            newRegion.setCountry( ECountry.getCountryById( region.getCountry().getId() ) );
            setRaws(newRegion, region.getRawMaterials().getRawMaterial());
            regionList.add(newRegion);
        }

        return regionList;
    }

    
    private void setRaws(Region newRegion, List<RawMaterialT> rawMaterial) {
        
        for (RawMaterialT rawMaterialT : rawMaterial) {
            ERawRegion productivity = ERawRegion.getRegionByErepublikId(rawMaterialT.getProductivity());
            if (rawMaterialT.getName().equals("Oil") )
                newRegion.setOil(productivity);
            if (rawMaterialT.getName().equals("Stone") )
                newRegion.setStone(productivity);
            if (rawMaterialT.getName().equals("Iron") )
                newRegion.setIron(productivity);
            if (rawMaterialT.getName().equals("Titanium") )
                newRegion.setTitanium(productivity);
            if (rawMaterialT.getName().equals("Grain") )
                newRegion.setGrain(productivity);
        }
        
    }
 
    
    
    public static void main(String[] args) {
        RegionsScanner scanner = new RegionsScanner();
        ArrayList<Region> regions = scanner.scannRegions();
        for (Region region : regions) {
            System.out.println(region.getName() + " " + region.getCountry());
            System.out.println("grain: "+region.getGrain());
            System.out.println("iron: "+region.getIron());
            System.out.println("oil: "+region.getOil());
            System.out.println("stone: "+region.getStone());
            System.out.println("titanium: "+region.getTitanium());
            System.out.println("");
        }
        try {
            saveRegionsToJSON(regions, "src/main/resources/regions.json");
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

    
    
    private static void saveRegionsToJSON(ArrayList<Region> regions, String path) throws JSONException, IOException {
        JSONArray jsonArray = new JSONArray();
        for (Region region : regions) {
            jsonArray.put(region.toJSONObject());
        }
        BufferedWriter out = new BufferedWriter(new FileWriter(new File(path)));
        out.write(jsonArray.toString());
        out.close();
        
    }
    
}
